define([
    "../../service.js",
    "pager",
    "../../assets/vendor/bootstrap/bootstrap-dropdown",
    "../../assets/vendor/jquery-ui-1.11.4.min",
    "../../assets/vendor/datatimepicker-language"
], function(api) {
    
    function getCookie(name)
    {  
        var nameEQ = name + "=";  
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) 
        {  
            var c = ca[i];  
            while(c.charAt(0)==' ')
            {
                c = c.substring(1,c.length);
            }
            if(c.indexOf(nameEQ) == 0) 
            {
                return unescape(c.substring(nameEQ.length,c.length));
            }
        }
        return false;  
    }

    function clearCookie(name)

    {  
        setCookie(name, "", -1);
    }

    function setCookie(name, value, seconds){  
        seconds = seconds || 0;  
        var expires = "";
        if(seconds != 0 ){
            var date = new Date();
            date.setTime(date.getTime()+(seconds*1000));  
            expires = ";expires="+date.toGMTString();
        }  
        document.cookie = name+"="+escape(value)+expires+";path=/";
    }
    var vm = avalon.define({
        $id: 'myRegister',
        recordVideoList: [],
        $setting: {},
        param: {
            method: api.getRegister,
            query: {
                PageSize: 12,
                PageIndex: 1
            }
        },
        zgbh: '',
        searchkey: '',
        startTime: '',
        endTime: '',
        logon: '',
        uri: '',
        nickname:'',
        showTable: false,
        defaultAlert:'无记录',
        delRec:function(id){
            var query = {
                method:api.delRegister,
                query:{}
            }
            if(vm.$setting.enableQQAuth){
                    query.query.parentEmployee = vm.uri;
            }else{
                query.query.parentEmployee = vm.zgbh;
            }
            query.query.id = id

            return api.getItems(query,function(res){
                if(res.success){
                    return vm.getRecordVideoList(vm.param)
                }
            })
        },
        logout:function(){
            vm.zgbm = ''
            vm.uri = ''
            vm.logon = ''
            vm.nickname = ''
            clearCookie('avanickname')
            clearCookie('avazgbm')
            clearCookie('avauri')
            avalon.router.go('liveRegister',{})
            // location.href = "#!/liveRegister"
        },
        getRecordVideoList: function(query, cb) {
            if(vm.$setting.enableQQAuth){
                if (vm.logon != '') {
                    vm.param.query.parentEmployee = vm.uri;
                }else{
                    vm.showTable = false;
                    return;
                }
            }else{
                vm.param.query.parentEmployee = vm.zgbh;
            }
            if(vm.param.query.parentEmployee!=''){
                vm.showTable = true;
                vm.defaultAlert = '加载中';
                return api.getItems(avalon.mix(vm.param), function(res) {

                    if (res.query) {
                        vm.pager.totalItems = res.query.recordCount;
                        vm.pager.currentPage = res.query.pageIndex;
                        vm.pager.perPages = res.query.pageSize;
                        vm.pager.pageCount = res.query.pageCount;
                        var listPager = avalon.vmodels.registerPager;

                        if(listPager){
                            listPager.totalItems = res.query.recordCount;
                            listPager.currentPage = res.query.pageIndex;
                            listPager.pageCount = res.query.pageCount;
                            listPager.perPages = res.query.pageSize;
                        }
                        if(cb){
                            cb(res.query)
                        }

                        // avalon.scan();
                        // console.log(vm.pager)
                    }
                    if (res.list) {
                        vm.recordVideoList = _.merge([], res.list)
                    }
                    if(vm.recordVideoList.length == 0){
                        vm.defaultAlert = '无记录'
                    }

                })
            }
            
        },
        pager: {
            currentPage: 1,
            perPages: 12,
            showJumper: false,
            alwaysShowNext: true, //@config {Boolean} 总是显示向后按钮
            alwaysShowPrev: true,
            pageCount: 1,
            totalItems:1,
            onJump: function(e, data) {
                vm.param.query.PageIndex = data.currentPage;
                vm.getRecordVideoList(vm.param, function(data) {
                    vm.pager.currentPage = data.pageIndex;
                });
            }
        },
       
        searchVideos: function() {}
    })
    vm.searchVideos = function() {

        vm.param.query.SearchKey = vm.searchkey;
        vm.param.query.startTimeBegin = vm.startTime
        vm.param.query.startTimeEnd = vm.endTime!=''?(vm.endTime+' 23:59:59'):''
        vm.getRecordVideoList(vm.param);
    }

    vm.$watch("setting", function(param) {
        vm.$setting = avalon.define(
            avalon.mix(vm.$setting, param))
        if(!vm.$setting.enableQQAuth){
            if(avalon.vmodels.app.currentStateModel.vmName=="myRegister"){
                vm.getRecordVideoList(vm.param)
            }
        }
    })
    vm.$watch("startTime",function(str){
         $("input[name='endTime']").datepicker("option", "minDate", str)
    })


    return avalon.controller(function($ctrl) {
        // 视图渲染后，意思是avalon.scan完成
        $ctrl.$onRendered = function() {

            $(".dateui").datepicker({
                "dateFormat": "yy-mm-dd"
            })
            $('.searchOpts input').on('keyup',function(evt){
                var e = evt||window.event;
                if(e.keyCode == 13){
                    vm.searchVideos()
                }
            })
             vm.getRecordVideoList(vm.param)
        };
        // 进入视图
        $ctrl.$onEnter = function(param, rs, rj) {
            
            
            // 获取登录状态
            if(getCookie('avazgbm')){
                vm.zgbh = getCookie('avazgbm')

                vm.logon = "stafflogon"
            }
            if(getCookie('avauri')){
                vm.uri = getCookie('avauri')
                vm.logon = "QQLogon"
            }
            if(getCookie('avanickname')){
                vm.nickname = getCookie('avanickname')
            }
           
            // console.log('send')

        };
        // 对应的视图销毁前
        $ctrl.$onBeforeUnload = function() {
            vm.logon = '';
            vm.showTable = false;
            var listPager = avalon.vmodels.listPager;

            if(listPager){
                listPager.totalItems = 0;
                listPager.currentPage = 1;
                listPager.pageCount = 0;
                listPager.perPages = 12;
            }

        };
        $ctrl.$vmodels = [vm];
    })
})
